<?php

class M_user extends CI_Model{
	
	function M_user(){
		parent::__construct();
	}
	
	function get_permission(){
		$query = "SELECT * FROM s_menus WHERE menu_parent<>0 AND menu_aktif='Y'";
		$result = $this->db->query($query);
		$nbrows = $result->num_rows();
		$query = "SELECT menu_id,menu_parent,menu_title,menu_position,1 as perm_read, 0 as perm_create,
		0 as perm_update, 0 as perm_delete FROM s_menus
		WHERE menu_parent=0 and menu_aktif='Y' ORDER By menu_position";
		$result = $this->db->query($query);
		
		$jsonresult = "";
		$data = array();
		if($nbrows>0){
			$i=0;
			foreach($result->result() as $row){
				$data[] = $row;
				
				$sql = "SELECT menu_id,menu_parent,menu_title,menu_position,0 as perm_read, 0 as perm_create,
				0 as perm_update, 0 as perm_delete FROM s_menus
				WHERE menu_parent='".$row->menu_id."' AND menu_aktif='Y' ORDER By menu_position";
				$rs_child=$this->db->query($sql);
				foreach($rs_child->result() as $rowchild){
					$data[] = $rowchild;
					
					/*$sql_perm="SELECT menu_id,menu_parent,menu_title,menu_position,perm_read,perm_create,perm_update,perm_delete
					FROM vu_s_permissions
					WHERE perm_menu='".$rowchild->menu_id."'
					and menu_aktif='Y'";
					$rs_perm=$this->db->query($sql_perm);
					if($rs_perm->num_rows()){
						$rowperm=$rs_perm->row();
						$data[] = $rowperm;
						$sql_perm="";
					}else
						$data[] = $rowchild;*/
				}
				$sql="";
				$i++;
			}
			return "{\"data\":" .json_encode($data). "}";
		} else {
			return "{\"data\":" .$jsonresult. "}";
		}
	}
	
	function get_permission_byuser($user_id){
		$query = "SELECT * FROM s_menus WHERE menu_parent<>0 AND menu_aktif='Y'";
		$result = $this->db->query($query);
		$nbrows = $result->num_rows();
		$query = "SELECT menu_id,menu_parent,menu_title,menu_position,1 as perm_read, 0 as perm_create,
		0 as perm_update, 0 as perm_delete FROM s_menus
		WHERE menu_parent=0 and menu_aktif='Y' ORDER By menu_position";
		$result = $this->db->query($query);
	
		$jsonresult = "";
		$data = array();
		if($nbrows>0){
			$i=0;
			foreach($result->result() as $row){
				$data[] = $row;
	
				$sql = "SELECT menu_id,menu_parent,menu_title,menu_position,0 as perm_read, 0 as perm_create,
				0 as perm_update, 0 as perm_delete FROM s_menus
				WHERE menu_parent='".$row->menu_id."' AND menu_aktif='Y' ORDER By menu_position";
				$rs_child=$this->db->query($sql);
				foreach($rs_child->result() as $rowchild){
					$sql_perm="SELECT menu_id,menu_parent,menu_title,menu_position,perm_read,perm_create,perm_update,perm_delete
						FROM vu_s_permissions
					WHERE perm_user = ".$user_id." AND perm_menu='".$rowchild->menu_id."'
					and menu_aktif='Y'";
					$rs_perm=$this->db->query($sql_perm);
					if($rs_perm->num_rows()){
					$rowperm=$rs_perm->row();
					$data[] = $rowperm;
					$sql_perm="";
					}else
						$data[] = $rowchild;
				}
				$sql="";
				$i++;
			}
			return "{\"data\":" .json_encode($data). "}";
		} else {
			return "{\"data\":" .$jsonresult. "}";
		}
	}
	
	function create($user_name, $user_pass, $user_group, $user_aktif){
		$sql = "SELECT user_name FROM s_users WHERE user_name='".$user_name."'";
		$rs = $this->db->query($sql);
		if($rs->num_rows()>0){
			return 0;
		}else{
			$data = array(
					"user_name"=>$user_name,
					"user_passwd"=>md5($user_pass),
					"user_groups"=>$user_group,
					"user_aktif"=>$user_aktif
			);
			$this->db->insert('s_users', $data);
			if($this->db->affected_rows())
				return 1;
			else
				return 0;
		}
	}
	
	function update($user_id, $user_name, $user_pass, $user_group, $user_aktif){
		$data = array(
				"user_name"=>$user_name,
				"user_aktif"=>$user_aktif
		);
		if($user_pass!=''){
			$data["user_passwd"] = md5($user_pass);
		}
		
		if(is_numeric($user_group)){
			$sql_group = "SELECT group_id FROM s_usergroups WHERE group_id=".$user_group;
			$rs_group = $this->db->query($sql_group);
			if($rs_group->num_rows()>0){
				$data['user_groups'] = $user_group;
			}
		}
		
		$this->db->where('user_id', $user_id);
		$this->db->update('s_users', $data);
		
		return 1;
	}
	
}

?>